package MetaClassifierAlgorithms;

import java.util.Arrays;

import Definitions.GraphClass;
import Global.GlobalClass;
import Sampling.SamplingAbstractClass;
import Utility.WorkerUtilityClass;

public class LocalBetaBasedMetaClassifierClass  extends MetaClassifierAbstractClass {

	LocalBetaBasedMetaClassifierClass(String metaClassifierName, GraphClass graph,
			GlobalClass global, SamplingAbstractClass currentSampling, double[][][] probabilityDistributionMatrixForTheClassifiers) {
		
		super(metaClassifierName, graph, global, currentSampling, probabilityDistributionMatrixForTheClassifiers);
		// TODO Auto-generated constructor stub
	}
	
	public void metaClassify()
	{
		double sumOfTheWeightedProbabilitiesPerClassBasis[] = new double[global.classSize];
		//double metaResultProbabilitiesPerClassBasis[] = new double[GlobalClass.classSize];
		double[][] weightsToBeUsed = currentSampling.getLocalBetaMatrixForThisFold();
		//double[][] weightsToBeUsed = new double[weightsToBeUsedTemp.length][weightsToBeUsedTemp[0].length]; //= currentSampling.getLocalAlphaMatrixForThisFold();
		
		double[] globalWeightsToBeUsed = new double[weightsToBeUsed.length];		
		
		int metaClassLabelEstimated;
	
		double sumOfTheLocalBetaWeigthtsForTheNode;
		
		//WorkerUtilityClass.initializeTwoDimensionalArray(weightsToBeUsed, 5.0);
		
		for(int j=0; j<probabilityDistributionMatrixForTheClassifiers.length; j++)
			globalWeightsToBeUsed[j] = Utility.WorkerUtilityClass.getAverage(weightsToBeUsed[j]);
		
		
		int[] numberOfLocalBetaUsagePerCls = new int[probabilityDistributionMatrixForTheClassifiers.length];
		int[] numberOfGlobalBetaUsagePerCls = new int[probabilityDistributionMatrixForTheClassifiers.length];
		int[] sumMethodLikeUsagePerCls = new int[probabilityDistributionMatrixForTheClassifiers.length];
		
		for(int nodeNo=0; nodeNo<probabilityDistributionMatrixForTheClassifiers[0].length; nodeNo++)
		{
			Arrays.fill(sumOfTheWeightedProbabilitiesPerClassBasis,0.0);
						
			sumOfTheLocalBetaWeigthtsForTheNode = 0.0;
			
			for(int j=0; j<probabilityDistributionMatrixForTheClassifiers.length; j++)
				sumOfTheLocalBetaWeigthtsForTheNode += weightsToBeUsed[j][nodeNo];
			
			for(int j=0; j<probabilityDistributionMatrixForTheClassifiers.length; j++)
			{	
				//System.out.println("["+j+"]["+nodeNo+"]:"+weightsToBeUsed[j][nodeNo]);
				if(sumOfTheLocalBetaWeigthtsForTheNode!=0)
				{
					sumOfTheWeightedProbabilitiesPerClassBasis = WorkerUtilityClass.getVectoralSumOfTheDoubleArrays(sumOfTheWeightedProbabilitiesPerClassBasis, WorkerUtilityClass.getScalarProductOfTheArray(probabilityDistributionMatrixForTheClassifiers[j][nodeNo], weightsToBeUsed[j][nodeNo]));
					numberOfLocalBetaUsagePerCls[j]++;
				}
				else if(globalWeightsToBeUsed[j]!=0)
				{
					sumOfTheWeightedProbabilitiesPerClassBasis = WorkerUtilityClass.getVectoralSumOfTheDoubleArrays(sumOfTheWeightedProbabilitiesPerClassBasis, WorkerUtilityClass.getScalarProductOfTheArray(probabilityDistributionMatrixForTheClassifiers[j][nodeNo], globalWeightsToBeUsed[j]));
					numberOfGlobalBetaUsagePerCls[j]++;
				}
				else
				{
					sumOfTheWeightedProbabilitiesPerClassBasis = WorkerUtilityClass.getVectoralSumOfTheDoubleArrays(sumOfTheWeightedProbabilitiesPerClassBasis, probabilityDistributionMatrixForTheClassifiers[j][nodeNo]);
					sumMethodLikeUsagePerCls[j]++;
				}
			}
			//metaResultProbabilitiesPerClassBasis = WorkerUtilityClass.scalarDivisionOfTheArray(sumOfTheWeightedProbabilitiesPerClassBasis, probabilityDistributionMatrixForTheClassifiers[0].length);
			
			
			//metaClassLabelEstimated = WorkerUtilityClass.findMaxIndexForTheGivenVector(probabilityDistributionMatrixForTheClassifiers[1][nodeNo]);
			metaClassLabelEstimated = WorkerUtilityClass.findMaxIndexForTheGivenVector(sumOfTheWeightedProbabilitiesPerClassBasis);
			currentSampling.setClassLabelEstimated(currentSampling.getTestNodes().get(nodeNo), metaClassLabelEstimated);			
		}		
		
		WorkerUtilityClass.printArray(numberOfLocalBetaUsagePerCls, "Local Beta Usage Stats:");
		WorkerUtilityClass.printArray(numberOfGlobalBetaUsagePerCls, "Global Beta Usage Stats:");
		WorkerUtilityClass.printArray(sumMethodLikeUsagePerCls, "Sum Method like Usage Stats:");
	}
}
